package v1

import (
	"github.com/gin-gonic/gin"
	"github.com/Unknwon/com"
	"github.com/astaxie/beego/validation"
	"blog/pkg/errmsg"
	"blog/models"
	"log"
	"net/http"
	"blog/pkg/utils"
	"blog/pkg/settings"
)

//获取单个文章
func GetArticle(c *gin.Context) {
	id, _ := com.StrTo(c.Param("id")).Int()

	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("Id必须大于0")
	code := errmsg.INVALID_PARAMS

	var data interface{}

	if ! valid.HasErrors() {
		if models.ExistArticleById(id) {
			data = models.GetArticle(id)
			code = errmsg.SUCCESS
		} else {
			code = errmsg.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetMsg(code),
		"data": data,
	})
}

//获取多个文章
func GetArticles(c *gin.Context) {
	data := make(map[string]interface{})
	maps := make(map[string]interface{})
	valid := validation.Validation{}

	state := -1

	if arg := c.Query("state"); arg != "" {
		state, _ = com.StrTo(arg).Int()
		maps["state"] = state

		valid.Range(state, 0, 1, "state")
	}

	tagId := -1

	if arg := c.Query("tagId"); arg != "" {
		tagId, _ = com.StrTo(arg).Int()
		maps["tagId"] = tagId

		valid.Min(tagId, 1, "tag_id").Message("标签ID必须大于0")
	}

	code := errmsg.INVALID_PARAMS

	if !valid.HasErrors() {
		code = errmsg.SUCCESS
		data["list"] = models.GetArticles(utils.GetPage(c), settings.PageSize, maps)
		data["total"] = models.GetTagTotal(maps)
	} else {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetMsg(code),
		"data": data,
	})
}

//新增文章
func AddArticle(c *gin.Context) {
	tagId, _ := com.StrTo(c.Query("tagId")).Int()
	title := c.Query("title")
	desc := c.Query("desc")
	content := c.Query("content")

	createdBy := c.Query("createdBy")
	state, _ := com.StrTo(c.DefaultQuery("state", "0")).Int()

	valid := validation.Validation{}
	valid.Min(tagId, 1, "tagId").Message("标签id必须大于0")
	valid.Required(title, "title").Message("标题不能为空")
	valid.Required(desc, "desc").Message("简述不能为空")
	valid.Required(content, "content").Message("内容不能为空")
	valid.Required(createdBy, "createdBy").Message("创建人不能为空")
	valid.Range(state, 0, 1, "state").Message("状态只允许0或者1")

	code := errmsg.INVALID_PARAMS

	if valid.HasErrors() {
		if models.ExistTagByID(tagId) {
			data := make(map[string]interface{})
			data["tagId"] = tagId
			data["title"] = title
			data["desc"] = desc
			data["content"] = content
			data["createdBy"] = createdBy
			data["state"] = state

			models.AddArticle(data)
			code = errmsg.SUCCESS
		} else {
			code = errmsg.ERROR_NOT_EXIST_TAG
		}
	} else {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetMsg(code),
		"data": make(map[string]interface{}),
	})
}

//修改文章
func EditArticle(c *gin.Context) {
	valid := validation.Validation{}

	id, _ := com.StrTo(c.Param("id")).Int()
	tagId, _ := com.StrTo(c.Query("tagId")).Int()
	title := c.Query("title")
	desc := c.Query("desc")
	content := c.Query("content")
	modifiedBy := c.Query("modifiedBy")

	state := -1
	if arg := c.Query("state"); arg != "" {
		state, _ = com.StrTo(arg).Int()
		valid.Range(state, 0, 1, "state").Message("状态只允许0或者1")
	}

	valid.Min(id, 1, "id").Message("Id必须大于0")
	valid.MaxSize(title, 100, "title").Message("标题最长100字符")
	valid.MaxSize(desc, 255, "desc").Message("简述最长为255字符")
	valid.MaxSize(content, 65535, "content").Message("内容最长为65535字符")
	valid.Required(modifiedBy, "modifiedBy").Message("修改人不能为空")
	valid.MaxSize(modifiedBy, 100, "modifiedBy").Message("修改人最长为100字符")

	code := errmsg.INVALID_PARAMS

	if ! valid.HasErrors() {
		if models.ExistArticleById(id) {
			if models.ExistTagByID(tagId) {
				data := make(map[string]interface{})
				if tagId > 0 {
					data["tagId"] = tagId
				}

				if title != "" {
					data["title"] = title
				}
				if desc != "" {
					data["title"] = title
				}

				if content != "" {
					data["content"] = content
				}
				data["modifiedBy"] = modifiedBy

				models.EditArticle(id, data)
				code = errmsg.SUCCESS
			} else {
				code = errmsg.ERROR_NOT_EXIST_TAG
			}
		} else {
			code = errmsg.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetMsg(code),
		"data": make(map[string]string),
	})
}

//删除文章
func DeleteArticle(c *gin.Context) {
	id, _ := com.StrTo(c.Param("id")).Int()

	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("id必须大于0")

	code := errmsg.INVALID_PARAMS

	if !valid.HasErrors() {
		if models.ExistArticleById(id) {
			models.DeleteArticle(id)
			code = errmsg.SUCCESS
		} else {
			code = errmsg.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetMsg(code),
		"data": make(map[string]string),
	})
}
